#include "recboxitem.h"
#include "ui_recboxitem.h"
#include<QPropertyAnimation>
#include<QDebug>

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);
    ui->musicImageBox->installEventFilter(this);
}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}
bool RecBoxItem::eventFilter(QObject *obj, QEvent *event)
{
    //只对图片进行过滤
    if(obj==ui->musicImageBox)
    {
        //当鼠标进入图片向上移动
        if(event->type()==QEvent::Enter)
        {
            //使用QPropertyAnimation来实现一些简单的动画
           QPropertyAnimation* animation=new QPropertyAnimation(obj,"geometry");
           animation->setDuration(2000);
           animation->setStartValue(QRect(9,9,ui->musicImageBox->width(),ui->musicImageBox->height()));
           animation->setEndValue(QRect(9,0,ui->musicImageBox->width(),ui->musicImageBox->height()));
           animation->start();

           //销毁动画对象的时机,当动画结束的时候会自动释放一个finished信号
           connect(animation,&QPropertyAnimation::finished,this,[=](){
               delete animation;
               qDebug()<<"图片上移结束";
           });
           return true;
        }
        else if(event->type()==QEvent::Leave)
        {
            //离开时的逻辑
            QPropertyAnimation* animation=new QPropertyAnimation(obj,"geometry");
            animation->setDuration(1000);
            animation->setStartValue(QRect(9,0,ui->musicImageBox->width(),ui->musicImageBox->height()));
            animation->setEndValue(QRect(9,9,ui->musicImageBox->width(),ui->musicImageBox->height()));
            animation->start();
            //销毁动画对象的时机,当动画结束的时候会自动释放一个finished信号
            connect(animation,&QPropertyAnimation::finished,this,[=](){
                delete animation;
                qDebug()<<"图片下移结束";
            });
            return true;
        }
    }
    //交给父类的事件拦截器
    return QObject::eventFilter(obj,event);
}


void RecBoxItem::setImage(QString &imagePath)
{
    QString imagestyle="background-image:url("+imagePath+")";
    ui->recMusicImage->setStyleSheet(imagestyle);
    ui->recMusicImage->setScaledContents(true);
}

void RecBoxItem::setText(QString &text)
{
    ui->recBoxItemText->setText(text);
}
